#ifndef   H_A2TRDEF
#define   H_A2TRDEF
#define   A2TRDEF_H

#ifndef H_A2SYSDEF
# include "a2sysdef.h"
#endif

#ifndef H_A2DIO
# include "a2dio.h"
#endif

#ifndef _LDSC_T
#define _LDSC_T
  struct linedsc {
      char           A2FAR *text;
      unsigned short      szline;
      struct linedsc A2FAR *down;
      struct linedsc A2FAR   *up;
      struct linedsc A2FAR *next;
    };
  typedef struct linedsc LDSC;
#endif

/*------------------------------------------------------------------*/
/* POSSIBLE MOVEMENT DIRECTIONS                                     */
/*------------------------------------------------------------------*/
/*            0                                                     */
#define  T_S  1   /* stack align - for DDT system only              */
#define  J_P  2   /* jump on reference - for ACS                    */
#define  D_R  3   /* jump to the root prolonging stack              */
#define  U_S  4   /* up stack                                       */
/*            5                                                     */
#define  U_H  6   /* up to the stack header                         */
#define  U_L  7   /* up to the level in the base                    */
#define  U_G  8   /* up to the level in the stack                   */
/*            9                                                     */
#define  L_K  10  /* movement on the level            using key     */
#define  L_F  11  /* to the first brother                           */
#define  L_L  12  /* to the last brother                            */
/*            13                                                    */
#define  L_P  14  /* to the previous brother                        */
#define  L_N  15  /* to the next brother                            */
#define  L_T  16  /* to the type - for DDT only                     */
#define  D_K  17  /* movement down, using key                       */
#define  D_F  18  /* to the first son                               */
#define  D_L  19  /* to the last son                                */
#define  D_T  20  /* to the type - for DDT only                     */
#define  S_O  21  /* stay on                                        */
/*            22                                                    */
/*            23                                                    */
/*------------------------------------------------------------------*/
/*  POSSIBLE  ACTIONS  IN  VERTICES                                 */
/*------------------------------------------------------------------*/
#define  N_A  24  /* no action                                      */
/*            25                                                    */
#define  U_V  26  /* update vertex                                  */
#define  U_K  27  /*        key                                     */
#define  U_D  28  /*        data                                    */
#define  R_V  29  /* read   vertex                                  */
#define  R_K  30  /*        key                                     */
#define  R_D  31  /*        data                                    */
#define  W_V  32  /* write  vertex                                  */
#define  W_K  33  /*        key                                     */
#define  W_D  34  /*        data                                    */
#define  D_V  35  /* delete vertex                                  */
#define  D_D  36  /*        data                                    */
#define  E_V  37  /* delete vertex with error indication            */
#define  E_D  38  /*        data                                    */
/*            39                                                    */
#define  C_V  40  /* copy vertex                                    */
#define  C_D  41  /* copy data (subtree)                            */
#define  M_V  42  /* move vertex                                    */
#define  CM_D 43  /* move data (subtree)                            */
/*                                                                  */
#define  D_P  44  /* duplicate process                              */
#define  R_C  45  /* delete copy of process                         */
#define  S_P  46  /* save process                                   */
#define  R_P  47  /* restore process                                */
/*                                                                  */
#define  A_T  48  /* add subtrees                                   */
#define  S_T  49  /* subtract subtrees                              */
#define  C_T  50  /* cross subtrees                                 */
/*                                                                  */
#define  O_P  51  /* open process                                   */
#define  C_P  52  /* close process                                  */
/*------------------------------------------------------------------*/


/*------------------------------------------------------------------*/
/* CODES OF THE VALUE AND KEY TYPES                                 */
/*------------------------------------------------------------------*/
/*                              in subsystems:  TREE   DDT   ACS    */
#define TY_M 0    /*    empty                    +      +     +     */
#define TY_T 1    /*    text                     +      +     +     */
#define TY_I 4    /*    long int                 +      +     +     */
#define TY_F 5    /*    float                    +      +     +     */
#define TY_D 6    /*    double                   +      +     +     */
#define TY_R 12   /*    reference to value              +     +     */
#define TY_P 14   /*    reference to pattern            +           */
#define TY_X 15   /*    long text                +      +     +     */
#define TY_S 17   /*    structure (subtree)      +      +     +     */
#define TY_A 18   /*    array                           +     +     */
#define TY_U 19   /*    unit                            reserved    */
#define TY_V 20   /*    void structure                  reserved    */
#define TY_B 32   /*    base type                             +     */
/*------------------------------------------------------------------*/

#ifndef _DEF_TREF
#define _DEF_TREF
typedef long TREF;           /* far reference in the DA file        */
#endif

typedef struct a2trstln {      /*---------stack line structure--------*/
   struct a2trstln A2FAR *sl;  /* pointer to previous line            */
   unsigned short          d;  /* global level number                 */
   char                    k;  /* vertex type: V_V - conv, V_O - net  */
   char                   gs;  /* pseudostate: TS_N,_A,_P,_B,_E,_M    */
   TREF                    r;       /*physical address of vertex      */
} STKLINE;

struct a2trcomm {             /*----------command structure----------*/
   char                   m;  /* direction of movement               */
   char                   d;  /* r/w operation type                  */
   char                  kt;  /* key type                            */
   char            A2FAR *k;  /* pointer to key                      */
   char                  vt;  /* value type                          */
   char            A2FAR *v;  /* pointer to value                    */
   struct a2trcomm A2FAR *p;  /* pointer to next struct.             */
};
typedef struct a2trcomm TCOMMAND;

/*------------------------------------------------------------------*/
/*         STRUCTURE OF PROCESS CONTROL BLOCK IN DATA TREE          */
/*------------------------------------------------------------------*/
struct a2trtree {
   char             cbname[4]; /* block header                      */
   DAFILE          A2FAR *pdf; /* pointer to buffer control block   */
   char             A2FAR *nf; /* pointer to file name              */
   struct a2trstln A2FAR  *sl; /* pnt. to current stack line        */
   struct a2trstln A2FAR *sl1; /* copy of stack (trans support)     */
   struct a2trtree A2FAR  *pt; /* next control block in list        */
   struct a2trtree A2FAR  *pf; /* nxt control block in the same file*/
   char              trid[12]; /* trace identifier                  */
   struct a2trtree A2FAR *trnxt; /* next in transaction list        */
   void          A2FAR   *ppd; /* address of process on server      */
   short                 blks; /* block size                        */
   TCOMMAND                cm; /* for parameters transferring       */
			char                   atr; /* index regime on/off               */
			short                  lvl; /* level number from tree root       */
			short                gllvl; /* level number in stack             */
			TREF                   msk; /* vertex address mask               */
			char                   nba; /* number of bits in vertex address  */
   char                  emsg; /* message level                     */
   char                   trc; /* trace level                       */
   char                  pack; /* dense writing regime              */
   char                     u; /* writing with replacing            */
   char                 vtree; /* process is copy                   */
   char             transType; /* type of transaction               */
   char                    ro; /* read only process                 */
};

typedef struct a2trtree TREE;
typedef TREE  A2FAR *  pTREE;

/*=================== superlong data descriptor ====================*/
typedef struct {
   long       lth;                                /* length of data */
   void A2FAR *xp;                                /* pnt. to data   */
} XDSC;

#ifndef __WATCOMC__
#if (__TURBOC__ >= 0x460) || (_MSC_VER >= 800)
# include <pshpack1.h>
#endif
#endif

/*= STRUCTURE FOR VERTEX DESCRIPTION IN DATA DESCRIPTION TREE ======*/
struct ddtdata {
   char     t; /*                      value type */
   long    ut; /*               ddt-key of father */
   long    ln; /* ddt-key of next vertex in level */
   union {
      long df; /*               first son ddt-key */
      long as; /*                 pattern ddt-key */
   } u;
   char pf[3]; /*                   output format */
   char     k; /*             key and index marks */
   char    kt; /*                        key type */
   short   ra; /*                     read access */
   short   wa; /*                    write access */
   long   res; /*                        reserved */
};

/*==== structure for information exchange with DDT storage =========*/
struct ddtfd {
   struct ddtdata d;  /* vertex description         */
   long         key;
   LDSC A2FAR   *ld;  /* LDSC with vertex name      */
   LDSC A2FAR  *ldt;  /* LDSC with path description */
};

typedef struct ddtfd DDTINF;

/*========= structure for internal information exchange ============*/
struct fxdata {
   unsigned long   l;  /* length of data (=sizeof(d))  */
   void     A2FAR *a;  /* address of data (=&d)        */
   struct  ddtdata d;  /* description data             */
   unsigned long  dk;  /* ddt - key                    */
};

struct accreftr {
   short nxt; /* next in the path list                */
   long   dk; /* ddt-key                              */
   char   kt; /* key type for key level               */
   char   fl; /* bits: 1-3  -  direction              */
              /*       4,5  -  nested path boundaries */
              /*         6  -  mark of key level      */
              /*         7  -  mark of key            */
};

#ifndef __WATCOMC__
#if (__TURBOC__ >= 0x460) || (_MSC_VER >= 800)
# include <poppack.h>
#endif
#endif

struct a2ddthsh {
   long         dk;       /* ddt-key of the vertex            */
   long        u_t;       /*            father vertex         */
   long        l_f;       /*            first on level        */
   long        l_l;       /*            last on level         */
   long        l_p;       /*            previous on level     */
   long        l_n;       /*            next on level         */
   long        d_f;       /*            first son             */
   long        d_l;       /*            last son              */
   long        org;       /* ddt-key of the original          */
   char       type;       /*                       value type */
   char          k;       /*              key and index marks */
   char      ktype;       /*                         key type */
   char      pf[3];       /*                    output format */
   LDSC  A2FAR *ld;       /*      pnt. to LDSC with text name */
   unsigned short   nxtk; /*    previous in collision on keys */
   unsigned short   pvsk; /*        next in collision on keys */
   unsigned short   nxtn; /*   previous in collision on names */
   unsigned short   pvsn; /*       next in collision on names */
   unsigned short nxtput; /*                        next used */
   unsigned short pvsput; /*                    previous used */
   long        res;       /*                            flags */
};


/*=== STRUCTURE OF PROCESS CONTROL BLOCK IN DATA DESCRIPTION TREE ==*/
struct a2ddtstr {
   char          cbname[4]; /* block header                         */
   TREE          A2FAR  *k; /* cnt. block of 1-st work process      */
   TREE          A2FAR  *n; /* cnt. block of 2-nd work process      */
   struct a2ddtstr A2FAR *pd; /* next control block in list         */
   struct a2ddtstr A2FAR *pf; /* next control block in the same file*/
   struct a2trstln A2FAR *sl; /* pnt. to current stack line         */
   char                trc; /* trace level                          */
   char           trid[12]; /* trace identifier                     */
   char              vtree; /* process is copy                      */
   short                vs; /* reserved                             */
   short               lvl; /* number of level from tree root       */
   short             gllvl; /* number of level from stack header    */
   TCOMMAND             cm; /* for parameters transferring          */
   char               emsg; /* message level                        */
   short             ifree; /* entry to free list in cash-table     */
   short            ifirst; /* first in queue to releasing          */
   short               icl; /* current line in cash-table           */
   char                 as; /* regime of ref. to pattern treatment  */
   char               base; /* data base exists                     */
   unsigned A2FAR *    enk; /* pnt. to table of entrees on ddt-key  */
   unsigned A2FAR *    enn; /* pnt. to table of entrees on txt.name */
   struct a2ddthsh vx [10]; /* an array of cash-table lines         */
};

typedef struct a2ddtstr DDT;

#ifndef H_A2NIKA
extern unsigned a2ddtbs;
#endif

/*======== structures for reference information storage ============*/
struct accref {
   long      dk; /* ddt-key of reference                            */
   short     nr; /* number of path description                      */
   short    beg; /* entry to path descr. list                       */
   TREE A2FAR *pt; /* cnt. block of process in data                 */
   short   nxtk; /* next in collision                               */
   short   pvsk; /* previous in collision                           */
   short   nxtp; /* next in queue for releasing                     */
   short   pvsp; /* prev. in queue for releasing                    */
   char      fl; /* &1 - translation required, &2 - to long path    */
};

#define A2RFNUM 10

/*======== STRUCTURE OF PROCESS CONTROL BLOCK IN DATA BASE =========*/
struct a2acc {
   char          cbname[4]; /* block header                         */
   TREE          A2FAR *pt; /* cnt.block of process in data tree    */
   DDT           A2FAR *pd; /* cnt.block of process in descr.tree   */
   char              vtree; /* process is copy                      */
   short             gllvl; /* current level from stack header      */
   short               lvl; /* current level from data tree root    */
   char             keylvl; /* mark of key level                    */
   char              keyno; /* mark of key vertex                   */
   char               type; /* value type                           */
   char              ktype; /* key type                             */
   long                 dk; /* ddt-key of vertex                    */
   char               emsg; /* message level                        */
   char                trc; /* trace level                          */
   char           trid[12]; /* trace identifier                     */
   char               rftr; /* regime of writing while ref passing  */
   char              rstop; /* regime of ref. vertex passing        */
   char                  u; /* writing with replacing               */
   char                atr; /* regime of index maintenance          */
   char                fl1; /* reserved                             */
   TCOMMAND             cm; /* for parameters transferring          */
   struct a2acc A2FAR *pch; /* chained process                      */
   short            minlvl; /* minimal level if chained prc.        */
   char                fch; /* flag on if chained                   */
   struct {
      char          ifreer; /* entry to free list in cash-table     */
      char          ifreet; /* entry to free list in path table     */
      char             icl; /* current line in the path table       */
      char          ifirst; /* first in queue to release            */
      struct accref   hr[A2RFNUM*1]; /* path table address          */
      struct accreftr tr[A2RFNUM*5]; /* path elements table address */
      unsigned short  en[A2RFNUM*2]; /* entriesy to the path table  */
   } ht;
};

typedef struct a2acc ACCESS;

/*------------------------------------------------------------------*/
/* CODES OF THE MESSAGE LEVELS                                      */
/*------------------------------------------------------------------*/
#define EM_S 1       /*          messages about serious errors only */
#define EM_P 2       /* about pseudostates in data description tree */
#define EM_T 4       /*             about pseudostates in data tree */

/*------------------------------------------------------------------*/
/*    CODES OF THE TRACE  LEVELS                                    */
/*------------------------------------------------------------------*/
#define TR_ON     1                   /*       put on minimum trace */
#define TR_PARM   2                   /* trace data input from user */
#define TR_STK    4                   /* trace stack                */
#define TR_BLK    8                   /* show blocks                */
#define TR_TRANS 16    /* trace transactions (1 activate this mode) */

#endif /* H_A2TRDEF */
